﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class ProductInStockAdd : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName = "BsProductinstock" + DateTime.Now.ToString("yyyyMMdd");
        private string m_strTbl = "BS_ProductInStock";
        private string m_strMainKey = "BillID";
        static bool m_IsNew = true;
        private DataTable DtProductInStockData
        {
            get
            {
                if (null == Session["ProductInStockData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["ProductInStockData"];
                }
            }
            set
            {
                Session["ProductInStockData"] = value;
            }
        }

        private DataTable CreateDT()
        {

            return BsProductinstock.CreateDT();
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtProductInStockData = null;
            SetControlAttr(new BsProductinstockEntity(), tBase);
            lblBillID.Text = ClsComm.GetBillCode(m_strTbl, m_strMainKey, "PI" + DateTime.Now.ToString("yyMMdd"), 3);
            lblBillDate.Text = DateTime.Now.ToString("yyyy-MM-dd");


        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                InitData();
            }
           
            Master.ValidationGroup = "gSubmit";
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }





        private void BindData()
        {


            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }





        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
            {
                return;
            }

            BsProductinstockEntity entity = new BsProductinstockEntity();

            entity.ScheduleID = GetString(txtScheduleID);
            entity.WorkOrderNo = GetString(txtWorkOrderNo);
            entity.ProductID = GetString(txtProductID);
            entity.Nums = CommonMethod.ConvertToDecimal(txtNums.Text);


            entity.BillDate = GetDateTime(lblBillDate);
            entity.BillID = GetString(lblBillID);
            entity.Status = "1";
            entity.CreateUserID = CurrentUser.UserID;
            entity.CreateUserName = CurrentUser.UserName;
            entity.CreateDate = DateTime.Now;



            BsProductinstock.SaveOrUpdate(entity, m_IsNew);
            base.LogAction(BaseEnum.ActionType.Add, entity.BillID, true, null, new EntityBase[] { entity });
            DtProductInStockData = null;
            UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../ProductStock/ProductInStockList.aspx");
        }




        #endregion

        #region 本地方法
        private bool ValidateData()
        {
            string strMention = "";

            if (m_IsNew && ClsComm.CheckExist(m_strTbl, m_strMainKey, lblBillID.Text.Trim(), true))
            {
                strMention += "表单编号已经存在！<br />";
            }
           



            decimal sum = CommonMethod.ConvertToDecimal(txtNums.Text);

            if (sum <= 0)
            {
                strMention += "数量必须大于零！<br />";
            }
            decimal lastSum = CommonMethod.ConvertToDecimal(txtLastNum.Text);
            if (sum > lastSum)
            {
                strMention += " 入库数量不能大于待入库数量";
            }

            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }
        #endregion

        protected void btnSelectProduct_Click(object sender, EventArgs e)
        {
           
                BindDetailData();
           

        }
        /// <summary>
        /// 取得内容，填到表格
        /// </summary>
        private void BindDetailData()
        {
            string strProductID = GetString(hidProductID);

            if (string.IsNullOrEmpty(strProductID))
            {
                return;
            }
            string strWorkOrderNo = GetString(hidWorkOrderNo);
            string strScheduleID = GetString(hidScheduleID);

            DataTable dt = BsProductinstock.SelectDialogList(lblBillID.Text, strProductID, strWorkOrderNo, strScheduleID);

            if (dt.Rows.Count != 0)
            {

                DataRow dr = dt.Rows[0];
                FillHead(dr);
                txtLastNum.Text = CommonMethod.GetString(dr["LastNum"]);

                txtNums.Text = CommonMethod.GetString(dr["LastNum"]);
            }
            BindData();
        }
        private void FillHead(DataRow dr)
        {
            txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
            txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
            txtProductID.Text = CommonMethod.GetString(dr["ProductID"]); ;
            txtProductName.Text = CommonMethod.GetString(dr["ProductName"]);
            txtSpecifications.Text = CommonMethod.GetString(dr["Specifications"]);
            txtUnit.Text = CommonMethod.GetString(dr["Unit"]);
            txtCustomerPartNo.Text = CommonMethod.GetString(dr["CustomerPartNo"]);
            txtOutSide.Text = CommonMethod.GetString(dr["OutSide"]);
            txtInSide.Text = CommonMethod.GetString(dr["InSide"]);
      
        }


    }
}

